﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Ro.AssistFunction.BackNodeInfo;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface.UserType;

namespace Ro.Interpreter.InterAssistFunc
{
    /// <summary>
    /// 泛型类
    /// 协助处理Tci信息
    /// 提取有效信息
    /// web
    /// 将tci信息直接提取到一个list列表内
    /// 返回的是ControlUseType
    /// </summary>
    public class ExtractTciInfo<T>

    {
        ///返回值
        public T BackType;

        /// <summary>
        /// 解析tci文件
        /// </summary>
        /// <param name="config">web对应TCI类，单个</param>
        public ExtractTciInfo(ConfigurationFile config)
        {
            //给定纯的 .tci文件  
            string compath;
            if (!config.Path.Contains("${"))
            {
                //Pro版本
                if (RoPubArgments.ProTips)
                {
                    compath = RoPubArgments.GuiTestArgsInfo.TciFolderPath + "/" + config.Path;
                }
                else
                {
                    compath = RoPubArgments.RoTestArgsInfo.TciFolderPath + "/" + config.Path;
                }
            }
            else
            {
                //返回已将参数转为值的tci路径 todo 5-4更新完成，支持多形式参数调用
                ArgsIntoValue argsIntoValue = new ArgsIntoValue(config.Path);
                compath = argsIntoValue.BackValue;//返回已将参数转为值的tci路径 todo 5-4更新完成，支持多形式参数调用
            }
            

            //解析Tci文件, 返回RoWeb.OneTciInfos，转换类型
            BackType = (T) Convert.ChangeType(AnalyzeTciFileMethod(compath, config.Id), typeof(T));
        }

        /// <summary>
        /// 私有函数
        /// 解析Web的Tci文件
        /// </summary>
        /// <param name="tcipath">tci路径</param>
        /// <param name="tciid">tci的Id</param>
        /// <returns>返回自定义RoWeb.ControlUseType类</returns>
        private RoWeb.ControlUseType AnalyzeTciFileMethod(string tcipath, string tciid)
        {
            //定义临时的返回值病初始化，写入ID值
            RoWeb.ControlUseType controlUseType = new RoWeb.ControlUseType
            {
                TciId = tciid,
                OneTciInfos = new List<RoWeb.OneTciInfo>()
            };

            //创建 xmldoc 完成tci解析
            //todo 更换为LINQ的方式解析XML文件，可以更快速的获取对应信息,可以直接跳过注释
            XElement root = XElement.Load(tcipath);
            //添加namespace
            XNamespace webinfo = "http://tempuri.org/RoTciForWeb.xsd";
            //查找一级子节点
            XElement firstele = root.Element(webinfo + "Resources");
            //反馈节点集
            if (firstele != null)
            {
                IEnumerable<XElement> templist = from var in firstele.Elements() select var;

                //解析
                foreach (XElement sig in templist)
                {
                    //定义一个onetciinfo,传入ControlUseType中
                    RoWeb.OneTciInfo oneTciInfo = new RoWeb.OneTciInfo();
                    if (sig != null)
                    {
                        oneTciInfo.ControlName = (from var in sig.Attributes() where var.Name == webinfo + "ID" select var).Single().Value;
                        XAttribute temp = (from var in sig.Attributes() where var.Name == webinfo + "Index" select var).SingleOrDefault();
                        oneTciInfo.Index = temp != null ? Convert.ToInt32(temp.Value) : 0;

                        XElement xElement = sig.Element(webinfo + "SearchProperty");
                        if (xElement != null)
                        {
                            oneTciInfo.Locator = (from var in xElement.Attributes() where var.Name == webinfo + "Locator" select var).Single().Value;
                            oneTciInfo.Value = xElement.Value;
                        }
                        else
                        {
                            oneTciInfo.Locator = string.Empty;
                            oneTciInfo.Value = string.Empty;
                        }

                        controlUseType.OneTciInfos.Add(oneTciInfo);
                    }
                }
            }
            return controlUseType;
        }
    }
}
